/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View.seguridad;

import Model.Perfil;
import Presenter.EnvioPedidoPresenter;
import Presenter.PerfilPresenter;
import Principals.Validadores;
import View.components.Index;
import java.util.List;
import javax.swing.JDesktopPane;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Vasco
 */
public class Frm_BuscarPerfil extends javax.swing.JDesktopPane {

    private DefaultTableModel mdlBuscarPerfil;
    Validadores val = new Validadores();

    /**
     * Creates new form Frm_BuscarPerfil
     */
    public Frm_BuscarPerfil() {
        EnvioPedidoPresenter.actualizarEnvioPedido();
        initComponents();

        val.desactivarAccionPegar(txtNombre);
        val.desactivarAccionPegar(txtDescripcion);

        tblBuscarPerfil.getTableHeader().setReorderingAllowed(false);
        mdlBuscarPerfil = new DefaultTableModel();
        String nombre = txtNombre.getText();
        String desc = txtDescripcion.getText();
        List<Perfil> lstPerfil;
        lstPerfil = PerfilPresenter.buscarPerfil(nombre, desc);
        mdlBuscarPerfil.setNumRows(0);
        mdlBuscarPerfil = (DefaultTableModel) tblBuscarPerfil.getModel();

        for (int i = 0; i < lstPerfil.size(); i++) {
            Perfil p = lstPerfil.get(i);
            Object[] row = new Object[]{p.getIdPerfil(), p.getNombre(), p.getDescripcion(), p.getEstado()};
            mdlBuscarPerfil.insertRow(i, row);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar1 = new javax.swing.JToolBar();
        btnToolNuevo1 = new javax.swing.JButton();
        btnToolModificar = new javax.swing.JButton();
        btnToolEliminar = new javax.swing.JButton();
        jSeparator3 = new javax.swing.JToolBar.Separator();
        btnToolLimpiar = new javax.swing.JButton();
        btnToolBuscar = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        btnToolCancelar = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JToolBar.Separator();
        pnlBuscarPerfil = new javax.swing.JPanel();
        lblNombre = new javax.swing.JLabel();
        lblDescripcion = new javax.swing.JLabel();
        txtNombre = new javax.swing.JTextField();
        txtDescripcion = new javax.swing.JTextField();
        btnBuscar = new javax.swing.JButton();
        pnlResultadosPerfil = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblBuscarPerfil = new javax.swing.JTable();

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);
        jToolBar1.setBorderPainted(false);

        btnToolNuevo1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/botones/Nuevo.png"))); // NOI18N
        btnToolNuevo1.setFocusable(false);
        btnToolNuevo1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnToolNuevo1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnToolNuevo1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToolNuevo1ActionPerformed(evt);
            }
        });
        jToolBar1.add(btnToolNuevo1);

        btnToolModificar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/botones/Editar.png"))); // NOI18N
        btnToolModificar.setFocusable(false);
        btnToolModificar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnToolModificar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnToolModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToolModificarActionPerformed(evt);
            }
        });
        jToolBar1.add(btnToolModificar);

        btnToolEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/botones/Eliminar.png"))); // NOI18N
        btnToolEliminar.setFocusable(false);
        btnToolEliminar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnToolEliminar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnToolEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToolEliminarActionPerformed(evt);
            }
        });
        jToolBar1.add(btnToolEliminar);
        jToolBar1.add(jSeparator3);

        btnToolLimpiar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/botones/Limpiar.png"))); // NOI18N
        btnToolLimpiar.setFocusable(false);
        btnToolLimpiar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnToolLimpiar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnToolLimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToolLimpiarActionPerformed(evt);
            }
        });
        jToolBar1.add(btnToolLimpiar);

        btnToolBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/botones/Buscar.png"))); // NOI18N
        btnToolBuscar.setEnabled(false);
        btnToolBuscar.setFocusable(false);
        btnToolBuscar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnToolBuscar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnToolBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToolBuscarActionPerformed(evt);
            }
        });
        jToolBar1.add(btnToolBuscar);
        jToolBar1.add(jSeparator1);

        btnToolCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/botones/Cancelar.png"))); // NOI18N
        btnToolCancelar.setEnabled(false);
        btnToolCancelar.setFocusable(false);
        btnToolCancelar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnToolCancelar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnToolCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToolCancelarActionPerformed(evt);
            }
        });
        jToolBar1.add(btnToolCancelar);
        jToolBar1.add(jSeparator2);

        pnlBuscarPerfil.setBorder(javax.swing.BorderFactory.createTitledBorder("Búsqueda"));

        lblNombre.setText("Nombre:");

        lblDescripcion.setText("Descripción:");

        txtNombre.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtNombreKeyTyped(evt);
            }
        });

        txtDescripcion.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtDescripcionKeyTyped(evt);
            }
        });

        btnBuscar.setText("Buscar");
        btnBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlBuscarPerfilLayout = new javax.swing.GroupLayout(pnlBuscarPerfil);
        pnlBuscarPerfil.setLayout(pnlBuscarPerfilLayout);
        pnlBuscarPerfilLayout.setHorizontalGroup(
            pnlBuscarPerfilLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlBuscarPerfilLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlBuscarPerfilLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlBuscarPerfilLayout.createSequentialGroup()
                        .addComponent(lblNombre)
                        .addGap(24, 24, 24)
                        .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(34, 34, 34)
                        .addComponent(lblDescripcion)
                        .addGap(18, 18, 18)
                        .addComponent(txtDescripcion, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlBuscarPerfilLayout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnBuscar)))
                .addContainerGap())
        );
        pnlBuscarPerfilLayout.setVerticalGroup(
            pnlBuscarPerfilLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlBuscarPerfilLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(pnlBuscarPerfilLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlBuscarPerfilLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblDescripcion)
                        .addComponent(txtDescripcion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(pnlBuscarPerfilLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblNombre)
                        .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addComponent(btnBuscar)
                .addGap(21, 21, 21))
        );

        pnlResultadosPerfil.setBorder(javax.swing.BorderFactory.createTitledBorder("Resultados"));

        tblBuscarPerfil.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Id Perfil", "Nombre", "Descripción"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(tblBuscarPerfil);

        javax.swing.GroupLayout pnlResultadosPerfilLayout = new javax.swing.GroupLayout(pnlResultadosPerfil);
        pnlResultadosPerfil.setLayout(pnlResultadosPerfilLayout);
        pnlResultadosPerfilLayout.setHorizontalGroup(
            pnlResultadosPerfilLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlResultadosPerfilLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2)
                .addContainerGap())
        );
        pnlResultadosPerfilLayout.setVerticalGroup(
            pnlResultadosPerfilLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlResultadosPerfilLayout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 514, Short.MAX_VALUE)
            .addComponent(pnlBuscarPerfil, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(pnlResultadosPerfil, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(pnlBuscarPerfil, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(pnlResultadosPerfil, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        getAccessibleContext().setAccessibleName("");
    }// </editor-fold>//GEN-END:initComponents

    private void cambiarVista(JDesktopPane panel) {
        ((Index) (this.getTopLevelAncestor())).changeDesktopPane(panel);
    }

    private void btnToolNuevo1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnToolNuevo1ActionPerformed
        cambiarVista(new Frm_RegistrarPerfil());
    }//GEN-LAST:event_btnToolNuevo1ActionPerformed

    private void btnToolModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnToolModificarActionPerformed
        int i = this.tblBuscarPerfil.getSelectedRow();
        if (i != -1) {
            int idPerfil = Integer.parseInt(this.tblBuscarPerfil.getValueAt(i, 0).toString());
            cambiarVista(new Frm_ModificarPerfil(idPerfil));
        } else {
            JOptionPane.showMessageDialog(this, "No se ha seleccionado ningún perfil.");
        }
    }//GEN-LAST:event_btnToolModificarActionPerformed

    private void btnToolBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnToolBuscarActionPerformed
    }//GEN-LAST:event_btnToolBuscarActionPerformed

    private void btnToolEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnToolEliminarActionPerformed
        int i = this.tblBuscarPerfil.getSelectedRow();
        if (i != -1) {
            int idPerfil = Integer.parseInt(this.tblBuscarPerfil.getValueAt(i, 0).toString());
            if (idPerfil != 1) {
                int respuesta = JOptionPane.showConfirmDialog(this, "¿Desea eliminar el perfil?", "Eliminar", JOptionPane.OK_CANCEL_OPTION);
                switch (respuesta) {
                    case JOptionPane.OK_OPTION:
                        if (!PerfilPresenter.buscarUsuariosEnPerfil(idPerfil)) {
                            PerfilPresenter.eliminarPerfil(idPerfil);
                            refrescar();
                        } else {
                            JOptionPane.showMessageDialog(this, "No se puede eliminar un perfil con usuarios asociados.");
                        }
                        break;
                    case JOptionPane.CANCEL_OPTION:
                        refrescar();
                        break;
                }
            } else {
                JOptionPane.showMessageDialog(this, "No se puede eliminar el perfil de administrador.");
            }

        } else {
            JOptionPane.showMessageDialog(this, "No se ha seleccionado ningún perfil.");
        }
    }//GEN-LAST:event_btnToolEliminarActionPerformed

    private void refrescar() {
        String nombre = txtNombre.getText();
        String desc = txtDescripcion.getText();
        List<Perfil> lstPerfil;
        lstPerfil = PerfilPresenter.buscarPerfil(nombre, desc);
        mdlBuscarPerfil.setNumRows(0);
        mdlBuscarPerfil = (DefaultTableModel) tblBuscarPerfil.getModel();

        for (int i = 0; i < lstPerfil.size(); i++) {
            Perfil p = lstPerfil.get(i);
            Object[] row = new Object[]{p.getIdPerfil(), p.getNombre(), p.getDescripcion(), p.getEstado()};
            mdlBuscarPerfil.insertRow(i, row);
        }
        tblBuscarPerfil.setVisible(true);
    }

    private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarActionPerformed
        String nombre = txtNombre.getText();
        String desc = txtDescripcion.getText();
        List<Perfil> lstPerfil;
        lstPerfil = PerfilPresenter.buscarPerfil(nombre, desc);
        mdlBuscarPerfil.setNumRows(0);
        mdlBuscarPerfil = (DefaultTableModel) tblBuscarPerfil.getModel();

        for (int i = 0; i < lstPerfil.size(); i++) {
            Perfil p = lstPerfil.get(i);
            Object[] row = new Object[]{p.getIdPerfil(), p.getNombre(), p.getDescripcion(), p.getEstado()};
            mdlBuscarPerfil.insertRow(i, row);
        }

        tblBuscarPerfil.setVisible(true);
        JOptionPane.showMessageDialog(this, "Se encontraron " + mdlBuscarPerfil.getRowCount() + " coincidencias");
    }//GEN-LAST:event_btnBuscarActionPerformed

    private void btnToolLimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnToolLimpiarActionPerformed
        mdlBuscarPerfil.setNumRows(0);
        mdlBuscarPerfil = (DefaultTableModel) tblBuscarPerfil.getModel();
        limpiar();
    }//GEN-LAST:event_btnToolLimpiarActionPerformed

    private void txtNombreKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtNombreKeyTyped
        val.validaCaracteresRaros(evt);
        val.validaMaxSize(evt, 20, txtNombre.getText());
    }//GEN-LAST:event_txtNombreKeyTyped

    private void txtDescripcionKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtDescripcionKeyTyped
        val.validaCaracteresRaros(evt);
        val.validaMaxSize(evt, 100, txtDescripcion.getText());
    }//GEN-LAST:event_txtDescripcionKeyTyped

    private void btnToolCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnToolCancelarActionPerformed
        //TODO código para cerrar la ventana
    }//GEN-LAST:event_btnToolCancelarActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBuscar;
    private javax.swing.JButton btnToolBuscar;
    private javax.swing.JButton btnToolCancelar;
    private javax.swing.JButton btnToolEliminar;
    private javax.swing.JButton btnToolLimpiar;
    private javax.swing.JButton btnToolModificar;
    private javax.swing.JButton btnToolNuevo1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JToolBar.Separator jSeparator2;
    private javax.swing.JToolBar.Separator jSeparator3;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JLabel lblDescripcion;
    private javax.swing.JLabel lblNombre;
    private javax.swing.JPanel pnlBuscarPerfil;
    private javax.swing.JPanel pnlResultadosPerfil;
    private javax.swing.JTable tblBuscarPerfil;
    private javax.swing.JTextField txtDescripcion;
    private javax.swing.JTextField txtNombre;
    // End of variables declaration//GEN-END:variables

    private void limpiar() {
        txtNombre.setText("");
        txtDescripcion.setText("");
    }
}
